package cn.boltzjf.sql4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public abstract class Table {
    
    private final String name;
    
    public Table(String name) {
        notNull(name, "Name should not be null");
        this.name = name;
    }

    public String name() {
        return name;
    }
    
    @Override
    public String toString() {
        return name;
    }
    
    private final Map<String, Column<?>> nameToColMap = new HashMap<>();
    private final ArrayList<Column<?>> columns = new ArrayList<>();
    
    public <T> Column<T> defineColumn(String name, Column.Type type, Class<T> javaType) {
        Column<T> col = new Column<T>(name, type, javaType);
        nameToColMap.put(name, col);
        columns.add(col);
        return col;
    }

    public Column<?> columnOf(String columnName) {
        return nameToColMap.get(columnName);
    }

    public Column<?>[] allColumns() {
        return columns.toArray(new Column<?>[nameToColMap.size()]);
    }
    
    private void notNull(Object obj, String msg) {
        if (obj == null) {
            throw new IllegalArgumentException(msg);
        }
    }
}
